﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml;
using System.Xml.Linq;
using YArchitech.Temp.Properties;

namespace YArchitech.Temp
{
	public static class HYDataAccess
	{
		private static string DuctTablename
		{
			get
			{
				return Resources.DuctTablename + ".xml";
			}
		}

		private static string DuctStandardFilename
		{
			get
			{
				return Path.Combine(HYDataAccess.DataFolder, HYDataAccess.DuctTablename);
			}
		}

		private static string PipeTablename
		{
			get
			{
				return Resources.PipeTablename + ".xml";
			}
		}

		private static string PipeStandardFilename
		{
			get
			{
				return Path.Combine(HYDataAccess.DataFolder, HYDataAccess.PipeTablename);
			}
		}

		private static string WaterPropertyTableName
		{
			get
			{
				return Resources.WaterPropertyTableName;
			}
		}

		public static string DataFolder { get; set; }

		public static void Init(bool overWriterData = false)
		{
			string path = Path.Combine(new DirectoryInfo(Assembly.GetExecutingAssembly().Location).Parent.Parent.FullName, "Data");
			if (overWriterData || !File.Exists(HYDataAccess.DuctStandardFilename))
			{
				File.Copy(Path.Combine(path, HYDataAccess.DuctTablename), HYDataAccess.DuctStandardFilename, true);
			}
			if (overWriterData || !File.Exists(HYDataAccess.PipeStandardFilename))
			{
				File.Copy(Path.Combine(path, HYDataAccess.PipeTablename), HYDataAccess.PipeStandardFilename, true);
			}
		}

		private static int GetPipeMaterials(out List<HYMaterial> hyMatls)
		{
			if (string.IsNullOrEmpty(HYDataAccess.DataFolder))
			{
				throw new Exception("HYDataAccess DataFolder is null");
			}
			hyMatls = new List<HYMaterial>();
			foreach (XElement xe in XElement.Load(Path.Combine(HYDataAccess.DataFolder, HYDataAccess.PipeTablename)).Elements("Material"))
			{
				HYMaterial hymaterial = new HYMaterial();
				hymaterial.ReadXml(xe);
				hyMatls.Add(hymaterial);
			}
			return hyMatls.Count;
		}

		private static int GetPipeMaterials(out List<HYMaterial> hyMatls, PipeType pipeType = PipeType.Pipe)
		{
			if (string.IsNullOrEmpty(HYDataAccess.DataFolder))
			{
				throw new Exception("HYDataAccess DataFolder is null");
			}
			hyMatls = new List<HYMaterial>();
			foreach (XElement xe2 in (from xe in XElement.Load(Path.Combine(HYDataAccess.DataFolder, HYDataAccess.PipeTablename)).Elements("Material")
			where (int)xe.Attribute("Type") == (int)pipeType
			select xe).Elements<XElement>())
			{
				HYMaterial hymaterial = new HYMaterial();
				hymaterial.ReadXml(xe2);
				hyMatls.Add(hymaterial);
			}
			return hyMatls.Count;
		}

		private static void SavePipeMaterials(List<HYMaterial> hyMatls)
		{
		}

		private static HYMaterial GetPipeMaterial(string materialName)
		{
			if (string.IsNullOrEmpty(HYDataAccess.DataFolder))
			{
				throw new Exception("HYDataAccess DataFolder is null");
			}
			IEnumerable<XElement> enumerable = from xe in XElement.Load(Path.Combine(HYDataAccess.DataFolder, HYDataAccess.PipeTablename)).Elements("Material")
			where xe.Attribute("Name").Value.ToString() == materialName
			select xe;
			if (enumerable == null || enumerable.Count<XElement>() == 0)
			{
				return null;
			}
			HYMaterial hymaterial = new HYMaterial();
			XElement xe2 = enumerable.First<XElement>();
			hymaterial.ReadXml(xe2);
			return hymaterial;
		}

		private static int GetPipeSpecifications(out HYPipeSpecifications specifications, string materialName)
		{
			if (string.IsNullOrEmpty(HYDataAccess.DataFolder))
			{
				throw new Exception("HYDataAccess DataFolder is null");
			}
			specifications = new HYPipeSpecifications();
			XElement xe2 = (from xe in XElement.Load(Path.Combine(HYDataAccess.DataFolder, HYDataAccess.PipeTablename)).Elements("Material")
			where xe.Attribute("Name").Value.ToString() == materialName
			select xe).First<XElement>();
			specifications.ReadXml(xe2);
			return specifications.Count;
		}

		private static int GetPipeSpecifications(out HYPipeSpecifications specifications, int matlId)
		{
			if (string.IsNullOrEmpty(HYDataAccess.DataFolder))
			{
				throw new Exception("HYDataAccess DataFolder is null");
			}
			specifications = new HYPipeSpecifications();
			XElement xe2 = (from xe in XElement.Load(Path.Combine(HYDataAccess.DataFolder, HYDataAccess.PipeTablename)).Elements("Material")
			where (int)xe.Attribute("MatlId") == matlId
			select xe).First<XElement>();
			specifications.ReadXml(xe2);
			return specifications.Count;
		}

		private static void SavePipeSpecifications(HYPipeSpecifications PipeSpecifications, int MatlId)
		{
			if (string.IsNullOrEmpty(HYDataAccess.DataFolder))
			{
				throw new Exception("HYDataAccess DataFolder is null");
			}
			string pipeStandardFilename = HYDataAccess.PipeStandardFilename;
			XElement xelement = XElement.Load(pipeStandardFilename);
			IEnumerable<XElement> enumerable = from xeMatl in xelement.Elements("Material")
			where xeMatl.GetIntAttribute("Id", 0) == MatlId
			select xeMatl;
			XElement xelement2;
			if (enumerable == null || enumerable.Count<XElement>() == 0)
			{
				xelement2 = new XElement("Material");
				xelement2.SetAttributeValue("Id", MatlId);
				xelement.Add(xelement2);
			}
			else
			{
				xelement2 = enumerable.First<XElement>();
				xelement2.RemoveNodes();
			}
			foreach (HYPipeSpecification hypipeSpecification in PipeSpecifications)
			{
				XElement xelement3 = new XElement("Specification");
				hypipeSpecification.WriteXml(xelement3);
				xelement2.Add(xelement3);
			}
			xelement.Save(pipeStandardFilename);
		}

		public static HYPipeSpecifications GetPipeSpecifications()
		{
			if (string.IsNullOrEmpty(HYDataAccess.DataFolder))
			{
				throw new Exception("HYDataAccess DataFolder is null");
			}
			HYPipeSpecifications hypipeSpecifications = new HYPipeSpecifications();
			XElement xe = XElement.Load(Path.Combine(HYDataAccess.DataFolder, HYDataAccess.PipeTablename));
			hypipeSpecifications.ReadXml(xe);
			return hypipeSpecifications;
		}

		public static void SavePipeSpecifications(HYPipeSpecifications specifications)
		{
			string text = Path.Combine(HYDataAccess.DataFolder, HYDataAccess.PipeTablename);
			XElement xelement = XElement.Load(text);
			xelement.RemoveNodes();
			specifications.WriteXml(xelement);
			xelement.Save(text);
		}

		public static bool GetDuctSpecifications(out HYDuctSpecifications ductSpecifications, HYShapeProfile secType = HYShapeProfile.Rectangular)
		{
			if (string.IsNullOrEmpty(HYDataAccess.DataFolder))
			{
				throw new Exception("HYDataAccess DataFolder is null");
			}
			HYDataAccess.Init(false);
			XContainer xcontainer = XElement.Load(HYDataAccess.DuctStandardFilename);
			ductSpecifications = new HYDuctSpecifications();
			XElement xe2 = (from xe in xcontainer.Elements("ShapeProfile")
			where (int)xe.Attribute("Value") == (int)secType
			select xe).First<XElement>();
			ductSpecifications.ReadXml(xe2);
			return ductSpecifications.Count > 0;
		}

		public static void SaveDuctSpecifications(HYDuctSpecifications RoundSpecifications, HYDuctSpecifications RectSpecifications)
		{
			if (string.IsNullOrEmpty(HYDataAccess.DataFolder))
			{
				throw new Exception("HYDataAccess DataFolder is null");
			}
			string ductStandardFilename = HYDataAccess.DuctStandardFilename;
			XElement xelement = XElement.Load(ductStandardFilename);
			foreach (XElement xelement2 in xelement.Elements("ShapeProfile"))
			{
				if ((int)xelement2.Attribute("Value") == 0)
				{
					HYDataAccess.SaveDuctSpecifications(RoundSpecifications, xelement2);
				}
				else if ((int)xelement2.Attribute("Value") == 1)
				{
					HYDataAccess.SaveDuctSpecifications(RectSpecifications, xelement2);
				}
			}
			xelement.Save(ductStandardFilename);
		}

		private static void SaveDuctSpecifications(HYDuctSpecifications ductSpecifications, XElement xeShape)
		{
			xeShape.RemoveNodes();
			foreach (HYDuctSpecification hyductSpecification in ductSpecifications)
			{
				XElement xelement = new XElement("Specification");
				hyductSpecification.WriteXml(xelement);
				xeShape.Add(xelement);
			}
		}

		private static string hyGetPath()
		{
			return Path.GetDirectoryName(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
		}

		private static string hyGetHelpSet()
		{
			return HYDataAccess.hyGetPath() + "\\Data\\HelpSet.xml";
		}

		public static string hyGetHelpChm()
		{
			return HYDataAccess.hyGetPath() + "\\Help\\HYRME.chm";
		}

		public static string hyGetHelpIndex(string tabName)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(HYDataAccess.hyGetHelpSet());
			return xmlDocument.SelectSingleNode("Helps/Help[@TabName='" + tabName + "']").Attributes["HelpIndex"].Value.ToString();
		}

		public static string GetPipeSystemCalIntro()
		{
			return HYDataAccess.hyGetPath() + "\\Data\\HYPipeSystemCal.txt";
		}

		public static string GetDuctSystemCalIntro()
		{
			return HYDataAccess.hyGetPath() + "\\Data\\HYDuctSystemCal.txt";
		}

		public static string GetHeatingSystemCalIntro()
		{
			return HYDataAccess.hyGetPath() + "\\Data\\HYDuctSystemCal.txt";
		}
	}
}
